import * as wasm from 'demo-wasm'

const webWork: Worker = self as any

function fib(val) {
  if (val === 1) {
    return 1;
  }
  if (val === 2) {
    return 2;
  }
  return fib(val - 1) + fib(val - 2)
}


webWork.onmessage = (ev) => {
  const data = ev.data
  console.log(data);
  if (data.dir === 'run') {
    let start = new Date().getTime()
    wasm.fib(data.value)
    let end = new Date().getTime()
    let wasmLong = end - start
    start = new Date().getTime()
    fib(data.value)
    end = new Date().getTime()
    let jsLong = end - start
    webWork.postMessage({
      jsLong,
      wasmLong
    })
  }
  if(data.dir==='close'){
    self.close()
  }
}
